package com.ywj.interview.thread.stopThread;

public class ThreadInterrupt extends Thread{

    @Override
    public void run() {
        try {
            sleep(40000);   //延迟40s
        } catch (InterruptedException e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
        }

//        while(!isInterrupted()) { //非阻塞过程中通过判断中断标志来退出
//            try {
//                Thread.sleep(5*1000);  //阻塞过程捕获中断异常来退出
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//                break;  //捕获到异常之后，执行break跳出循环
//            }
//        }
    }

    public static void main(String[] args) throws Exception{
        ThreadInterrupt t1 = new ThreadInterrupt();
        t1.start();
        System.out.println("在40s之内按任意键中断线程!");
        System.in.read();
        t1.interrupt();
        t1.join();
        System.out.println("线程已经退出！");
    }
}
